package cn.edu.zut.soft.student.service.notice.impl;

import cn.edu.zut.soft.student.bean.PageBean;
import cn.edu.zut.soft.student.dao.NoticeMapper;
import cn.edu.zut.soft.student.model.Notice;
import cn.edu.zut.soft.student.model.NoticeExample;
import cn.edu.zut.soft.student.service.notice.NoticeBo;
import cn.edu.zut.soft.student.service.notice.vo.NoticeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ChrisPhantom
 * @CreatDate 2021/02/16 10:58
 */

@Service
public class NoticeBoImpl implements NoticeBo {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public int addNotice(Notice notice) {
        return noticeMapper.insert(notice);
    }

    @Override
    public int deleteNotice(int id) {
        return noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateNotice(Notice notice) {
        return noticeMapper.updateByPrimaryKey(notice);
    }

    @Override
    public Notice getNoticeById(int noticeId) {
        return noticeMapper.selectByPrimaryKey(noticeId);
    }

    @Override
    public PageBean<List<Notice>> getNotices(NoticeQuery noticeQuery) {
        NoticeExample noticeExample = new NoticeExample();
        noticeExample.setOffset(noticeQuery.getOffset());
        noticeExample.setLimit(noticeQuery.getLimit());
        NoticeExample.Criteria criteria = noticeExample.createCriteria();
        if (noticeQuery.getLevel() != null && noticeQuery.getLevel() != "") {
            criteria.andLevelEqualTo(noticeQuery.getLevel());
        }
        noticeExample.setOrderByClause("level asc");
        List<Notice> notices = noticeMapper.selectByExample(noticeExample);
//        notices = notices.stream().sorted(Comparator.comparingInt(Notice::getIntegerLevel)).collect(Collectors.toList());
        return new PageBean<>(notices, noticeMapper.countByExample(noticeExample));
    }
}
